package cc.comac.mom.datahandlers.entity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.comac.mom.bean.QualityQarBasicStandardCheck;
import cc.comac.mom.bean.QualityQarBasicStandardRequirement;
import cc.comac.mom.bean.QualityReject;
import cc.comac.mom.bean.pclr.QualityPclrTestInvoiceEntityBean;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.dao.entity.QualityPclrTestInvoiceDetailDao;
import cc.comac.mom.entity.*;
import cc.comac.mom.util.*;
import cc.comac.mom.util.status.PclrStatusMap;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityPclrTestInvoice;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityPclrTestInvoiceDao;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class QualityPclrTestInvoiceDataHandler
		extends ServiceImpl<QualityPclrTestInvoiceDao, QualityPclrTestInvoiceEntity> {

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	@Autowired
	private QualityRejectDataHandler qualityRejectDataHandler;
	@Autowired
	private QualityPclrTestInvoiceDao qualityPclrTestInvoiceDao;

	@Autowired
	private OSSService oSSService;
	@Autowired
	private QualityTestContentDataHandler qualityTestContentDataHandler;
	@Autowired
	private QualityPclrTestInvoiceDataDetailHandler qualityPclrTestInvoiceDataDetailHandler;
	// 新增pclr实验单

	public ResultBean insertBatch(List<QualityPclrTestInvoice> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QualityPclrTestInvoiceEntity> listEntity = new ArrayList<QualityPclrTestInvoiceEntity>();
		ArrayList<QualityPclrTestInvoiceDetailEntity> detailListEntity = new ArrayList<QualityPclrTestInvoiceDetailEntity>();
		for (QualityPclrTestInvoice item : list) {
			QualityPclrTestInvoiceEntity innerData = item.getInnerData();
			if (innerData == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			baseEntityDataUtil.setIdNotNullBaseEntityData(innerData);
			listEntity.add(innerData);
			if(item.getDetailEntityList()!=null&&item.getDetailEntityList().size()>0){
                for(QualityPclrTestInvoiceDetailEntity detail:item.getDetailEntityList()){
                	detail.setTestInvoiceId(innerData.getId());
					baseEntityDataUtil.setIdNotNullBaseEntityData(detail);
					detailListEntity.add(detail);
				}
			}
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(detailListEntity.size()>0){
			qualityPclrTestInvoiceDataDetailHandler.saveBatch(detailListEntity);
		}
		if (!saveBatch) {
			resultBean.setErrorMessge("批量保存PCLR检验单失败");
			return resultBean;
		}
		return resultBean;
	}

	
	public ResultBean batchInsertPclrData (List<QualityPclrTestInvoice> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			return resultBean;
		}
		ArrayList<QualityPclrTestInvoiceEntity> listEntity = new ArrayList<QualityPclrTestInvoiceEntity>();
		for (QualityPclrTestInvoice item : list) {
			QualityPclrTestInvoiceEntity innerData = item.getInnerData();
			if (innerData == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			baseEntityDataUtil.setIdNotNullBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if (!saveBatch) {
			resultBean.setErrorMessge("批量保存PCLR检验单失败");
			return resultBean;
		}
		return resultBean;
	}
	// 批量修改状态
	public ResultBean updateStatusBatch(List<QualityPclrTestInvoice> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		ArrayList<QualityPclrTestInvoiceEntity> listEntity = new ArrayList<QualityPclrTestInvoiceEntity>();
		for (QualityPclrTestInvoice item : list) {
			QualityPclrTestInvoiceEntity innerData = item.getInnerData();
			if (innerData == null) {
				resultBean.setErrorMessge("数据为空");
				return resultBean;
			}
			baseEntityDataUtil.updateBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean updateBatchById = this.updateBatchById(listEntity);
		if (!updateBatchById) {
			resultBean.setErrorMessge("批量修改失败");
			return resultBean;
		}
		return resultBean;
	}
	// 添加实验分类

	public ResultBean updateTestClassify(QualityPclrTestInvoice obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		QualityPclrTestInvoiceEntity innerData = obj.getInnerData();
		String testNo = innerData.getTestNo();
		if (testNo == null || "".equals(testNo)) {
			resultBean.setErrorMessge("实验编号为空");
			return resultBean;
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityPclrTestInvoiceEntity> selectByMap = qualityPclrTestInvoiceDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			resultBean.setErrorMessge("无法根据实验编号：" + testNo + "获取实验单信息");
			return resultBean;
		}
		QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = selectByMap.get(0);
		qualityPclrTestInvoiceEntity.setTestClassify(innerData.getTestClassify());
		baseEntityDataUtil.updateBaseEntityData(qualityPclrTestInvoiceEntity);
		int updateById = qualityPclrTestInvoiceDao.updateById(qualityPclrTestInvoiceEntity);
		if (updateById < 1) {
			resultBean.setErrorMessge("修改实验分类失败");
			return resultBean;
		}
		return resultBean;
	}

	// 收料完成
	public ResultBean receiveCompletedByTestNo(QualityPclrTestInvoice obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}
		String testNo = obj.getInnerData().getTestNo();
		if (testNo == null || "".equals(testNo)) {
			resultBean.setErrorMessge("实验编号为空");
			return resultBean;
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityPclrTestInvoiceEntity> selectByMap = qualityPclrTestInvoiceDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			resultBean.setErrorMessge("实验单信息不存在");
			return resultBean;
		}
		QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = selectByMap.get(0);
		qualityPclrTestInvoiceEntity.setStatus(MomConstants.QUALITY_PCLR_STATUS_TESTING);
		qualityPclrTestInvoiceEntity.setReceiveUser(obj.getInnerData().getReceiveUser());
		qualityPclrTestInvoiceEntity.setDeliverUser(obj.getInnerData().getDeliverUser());
		qualityPclrTestInvoiceEntity.setDeliverDate(TimeUtil.getLocalDate());
		baseEntityDataUtil.updateBaseEntityData(qualityPclrTestInvoiceEntity);
		int updateById = qualityPclrTestInvoiceDao.updateById(qualityPclrTestInvoiceEntity);
		if (updateById < 1) {
			resultBean.setErrorMessge("收料完成失败");
			return resultBean;
		}
		return resultBean;

	}

	// 开始实验
	public ResultBean beginTestByTestNo(String testNo) {
		ResultBean resultBean = new ResultBean(true);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityPclrTestInvoiceEntity> selectByMap = qualityPclrTestInvoiceDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			resultBean.setErrorMessge("实验单信息不存在");
			return resultBean;
		}
		QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = selectByMap.get(0);
		qualityPclrTestInvoiceEntity.setStatus(MomConstants.QUALITY_PCLR_STATUS_TESTING);
		baseEntityDataUtil.updateBaseEntityData(qualityPclrTestInvoiceEntity);
		int updateById = qualityPclrTestInvoiceDao.updateById(qualityPclrTestInvoiceEntity);
		if (updateById < 1) {
			resultBean.setErrorMessge("开始实验失败");
			return resultBean;
		}
		return resultBean;
	}

	// 驳回按钮
	public ResultBean submitReject(QualityReject QualityReject) {
		ResultBean resultBean = new ResultBean(true);
		QualityRejectEntity innerData = QualityReject.getInnerData();
		if (StringUtils.isEmpty(innerData.getFormNo())) {
			throw new RuntimeException("无法获取实验单号");
		}
		// 查询检验标序号对应数据
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("test_no", innerData.getFormNo());
		QualityPclrTestInvoiceEntity selectOne = qualityPclrTestInvoiceDao.selectOne(queryWrapper);
		// 驳回前后的状态
		String rejectBefore = selectOne.getStatus();
		String rejectAfter = QualityReject.getInnerData().getRejectEndStatus();
		// 更改驳回状态
		if (selectOne.getStatus().equals(MomConstants.QUALITY_PCLR_STATUS_COMPLETE)) {
			throw new RuntimeException("归档完成状态无法驳回");
		} else if (selectOne.getStatus().equals(MomConstants.QUALITY_PCLR_STATUS_DISPATCH)) {
			throw new RuntimeException("调度中状态无法驳回");
		}
		// 记录驳回历史
		selectOne.setStatus(rejectAfter);
		QualityReject.getInnerData().setRejectBeforeStatus(rejectBefore);
		QualityReject.getInnerData().setRejectEndStatus(rejectAfter);
		QualityReject.getInnerData().setRejectTime(TimeUtil.getLocalDate());
		baseEntityDataUtil.setBaseEntityData(innerData);
		qualityRejectDataHandler.insert(QualityReject);

		// 更新实验表单状态
		qualityPclrTestInvoiceDao.updateById(selectOne);
		return resultBean;
	}

	// 提交审核
	public ResultBean submitAuditByTestNo(String testNo) {
		ResultBean resultBean = new ResultBean(true);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityPclrTestInvoiceEntity> selectByMap = qualityPclrTestInvoiceDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			resultBean.setErrorMessge("实验单信息不存在");
			return resultBean;
		}
		QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = selectByMap.get(0);
		qualityPclrTestInvoiceEntity.setStatus(MomConstants.QUALITY_PCLR_STATUS_AUDITING);
		baseEntityDataUtil.updateBaseEntityData(qualityPclrTestInvoiceEntity);
		int updateById = qualityPclrTestInvoiceDao.updateById(qualityPclrTestInvoiceEntity);
		if (updateById < 1) {
			resultBean.setErrorMessge("提交审核失败");
			return resultBean;
		}
		return resultBean;
	}

	// 审核完成
	public ResultBean auditCompletedByTestNo(String testNo) {
		ResultBean resultBean = new ResultBean(true);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityPclrTestInvoiceEntity> selectByMap = qualityPclrTestInvoiceDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			resultBean.setErrorMessge("实验单信息不存在");
			return resultBean;
		}
		QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = selectByMap.get(0);
		qualityPclrTestInvoiceEntity.setStatus(MomConstants.QUALITY_PCLR_STATUS_COMPLETE);
		baseEntityDataUtil.updateBaseEntityData(qualityPclrTestInvoiceEntity);
		int updateById = qualityPclrTestInvoiceDao.updateById(qualityPclrTestInvoiceEntity);
		if (updateById < 1) {
			resultBean.setErrorMessge("审核失败");
			return resultBean;
		}
		return resultBean;
	}

	// 通过实验编号获取试验单据信息
	public ResultBean getByTestNo(String testNo) {
		ResultBean resultBean = new ResultBean(true);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityPclrTestInvoiceEntity> selectByMap = qualityPclrTestInvoiceDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			resultBean.setErrorMessge("实验单信息不存在");
			return resultBean;
		}
		QualityPclrTestInvoice qualityPclrTestInvoice = new QualityPclrTestInvoice();
		QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = selectByMap.get(0);
		qualityPclrTestInvoice.setInnerData(qualityPclrTestInvoiceEntity);
		resultBean.setObject(qualityPclrTestInvoice);
		return resultBean;
	}

	// 查詢
	public LimitPageBean<QualityPclrTestInvoiceEntity> selectByCondition(QualityPclrTestInvoice obj) {
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getTestNo() != null && !"".equals(obj.getInnerData().getTestNo())) {
				queryWrapper.like("test_no", obj.getInnerData().getTestNo());
			}
			if (obj.getInnerData().getProductModel() != null && !"".equals(obj.getInnerData().getProductModel())) {
				queryWrapper.like("product_model", obj.getInnerData().getProductModel());
			}
			if (obj.getInnerData().getMaterialType() != null && !"".equals(obj.getInnerData().getMaterialType())) {
				queryWrapper.like("material_type", obj.getInnerData().getMaterialType());
			}
			if (obj.getInnerData().getMaterialDetail() != null && !"".equals(obj.getInnerData().getMaterialDetail())) {
				queryWrapper.like("material_detail", obj.getInnerData().getMaterialDetail());
			}
			if (obj.getInnerData().getOrderNo() != null && !"".equals(obj.getInnerData().getOrderNo())) {
				queryWrapper.like("order_no", obj.getInnerData().getOrderNo());
			}
			if (obj.getInnerData().getStatus() != null && !"".equals(obj.getInnerData().getStatus())) {
				queryWrapper.like("status", obj.getInnerData().getStatus());
			}
			if (obj.getInnerData().getIsConform() != null && !"".equals(obj.getInnerData().getIsConform())) {
				queryWrapper.like("is_conform", obj.getInnerData().getIsConform());
			}

			if (obj.getToFileDateBegin() != null) {
				queryWrapper.gt("to_file_date", obj.getToFileDateBegin());
			}
			if (obj.getToFileDateEnd() != null) {
				queryWrapper.lt("to_file_date", obj.getToFileDateEnd());
			}

		}
		Page<QualityPclrTestInvoiceEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<QualityPclrTestInvoiceEntity> iPage = qualityPclrTestInvoiceDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<QualityPclrTestInvoiceEntity> limitPageBean = (LimitPageBean<QualityPclrTestInvoiceEntity>) LimitPageBean
				.of(iPage);
		return limitPageBean;

	}

	// 查詢
	public List<QualityPclrTestInvoiceEntity> selectAllByCondition(QualityPclrTestInvoice obj) {
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getTestNo() != null && !"".equals(obj.getInnerData().getTestNo())) {
				queryWrapper.like("test_no", obj.getInnerData().getTestNo());
			}
			if (obj.getInnerData().getProductModel() != null && !"".equals(obj.getInnerData().getProductModel())) {
				queryWrapper.like("product_model", obj.getInnerData().getProductModel());
			}
			if (obj.getInnerData().getMaterialType() != null && !"".equals(obj.getInnerData().getMaterialType())) {
				queryWrapper.like("material_type", obj.getInnerData().getMaterialType());
			}
			if (obj.getInnerData().getMaterialDetail() != null && !"".equals(obj.getInnerData().getMaterialDetail())) {
				queryWrapper.like("material_detail", obj.getInnerData().getMaterialDetail());
			}
			if (obj.getInnerData().getOrderNo() != null && !"".equals(obj.getInnerData().getOrderNo())) {
				queryWrapper.like("order_no", obj.getInnerData().getOrderNo());
			}
			if (obj.getInnerData().getStatus() != null && !"".equals(obj.getInnerData().getStatus())) {
				queryWrapper.like("status", obj.getInnerData().getStatus());
			}
			if (obj.getInnerData().getIsConform() != null && !"".equals(obj.getInnerData().getIsConform())) {
				queryWrapper.like("is_conform", obj.getInnerData().getIsConform());
			}

			if (obj.getToFileDateBegin() != null) {
				queryWrapper.gt("to_file_date", obj.getToFileDateBegin());
			}
			if (obj.getToFileDateEnd() != null) {
				queryWrapper.lt("to_file_date", obj.getToFileDateEnd());
			}

		}
		List<QualityPclrTestInvoiceEntity> selectList = qualityPclrTestInvoiceDao.selectList(queryWrapper);
		return selectList;
	}

	public String exportData(HttpServletRequest request, HttpServletResponse response, QualityPclrTestInvoice obj) {
		List<QualityPclrTestInvoiceEntity> selectAllByCondition = selectAllByCondition(obj);

		// excel中对应的表头
		String[] rowName = new String[] { "实验编号", "材料说明", "委托时间", "完成时间", "状态" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity : selectAllByCondition) {
			Object[] objs = new Object[rowName.length];
			objs[0] = qualityPclrTestInvoiceEntity.getQarNo();
			objs[1] = qualityPclrTestInvoiceEntity.getMaterialDetail();
			objs[2] = qualityPclrTestInvoiceEntity.getCreationTime();
			objs[3] = qualityPclrTestInvoiceEntity.getToFileDate();
			objs[4] = PclrStatusMap.getStatusCN(qualityPclrTestInvoiceEntity.getStatus());
			dataList.add(objs);
		}

		String fileName = "实验单据查询数据";
		// 执行导出
		HSSFWorkbook exportExcel = ExportExcel.exportExcel(request, response, fileName, rowName, dataList,
				"yyyy-MM-dd HH:mm:ss");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			exportExcel.write(baos);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		String updateSheet = oSSService.updateSheet(swapStream, fileName);
		return updateSheet;
	}

	// 查詢
	public QualityPclrTestInvoiceEntity selectAllByTestNo(String testNo) {
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("row_status", 1);
		if (testNo != null && !"".equals(testNo)) {
			queryWrapper.like("test_no", testNo);
		}
		queryWrapper.like("row_status", 1);
		QualityPclrTestInvoiceEntity selectList = qualityPclrTestInvoiceDao.selectOne(queryWrapper);
		return selectList;
	}

	// 打印PCLR表单
	public PclrTestInvoicePrint printPclrTestInvoice(String testNo) throws Exception {

		if (StringUtils.isEmpty(testNo)) {
			throw new RuntimeException("无法获取PCLR表单编号！");
		}
		QualityPclrTestInvoiceEntity selectAllByTestNo = this.selectAllByTestNo(testNo);
		if (selectAllByTestNo == null) {
			throw new RuntimeException("无法获取检验单数据！");
		}
		PclrTestInvoicePrint pclrTestInvoicePrint = new PclrTestInvoicePrint();

		String auditDateString = "";
		if (selectAllByTestNo.getAuditDate() != null) {
			Date fomatLocalDateTimeToDate = selectAllByTestNo.getAuditDate();
			auditDateString = TimeUtil.fomatDate(fomatLocalDateTimeToDate);
		}
		pclrTestInvoicePrint.setApproved(selectAllByTestNo.getAuditUser() + " " + auditDateString);
		String deliverDateString = "";
		if (selectAllByTestNo.getDeliverDate() != null) {
			Date fomatLocalDateTimeToDate1 = selectAllByTestNo.getDeliverDate();
			deliverDateString = TimeUtil.fomatDate(fomatLocalDateTimeToDate1);
		}
		pclrTestInvoicePrint.setDeliverDate(deliverDateString);
		pclrTestInvoicePrint.setDeliverUser(selectAllByTestNo.getDeliverUser());
		pclrTestInvoicePrint.setDepartment(selectAllByTestNo.getDepartment());
		pclrTestInvoicePrint.setIsConform(selectAllByTestNo.getIsConform());
		pclrTestInvoicePrint.setIsExtraUrgent(selectAllByTestNo.getIsExtraUrgent() == 1 ? "是" : "否");
		pclrTestInvoicePrint.setIsUrgent(selectAllByTestNo.getIsUrgent() == 1 ? "是" : "否");
		pclrTestInvoicePrint.setLotSize(selectAllByTestNo.getLotSize().toString());
		pclrTestInvoicePrint.setMaterialDetail(selectAllByTestNo.getMaterialDetail());

		String needCompleteDateString = selectAllByTestNo.getNeedCompleteDate() == null ? ""
				: TimeUtil.fomatDate(selectAllByTestNo.getNeedCompleteDate());
		pclrTestInvoicePrint.setNeedCompleteDate(needCompleteDateString);

		pclrTestInvoicePrint.setOrderNo(selectAllByTestNo.getOrderNo());
		pclrTestInvoicePrint.setPclrNo(selectAllByTestNo.getTestNo());
		pclrTestInvoicePrint.setPhone(selectAllByTestNo.getPhone());
		pclrTestInvoicePrint.setPrepareUser(selectAllByTestNo.getPrepareUser());
		pclrTestInvoicePrint.setQarNo(selectAllByTestNo.getQarNo());
		pclrTestInvoicePrint.setReceiveUser(selectAllByTestNo.getReceiveUser());
		pclrTestInvoicePrint.setSeller(selectAllByTestNo.getSeller());
		pclrTestInvoicePrint.setStatus(selectAllByTestNo.getStatus());
		pclrTestInvoicePrint.setTestClassify(selectAllByTestNo.getTestClassify());
		String testDateString = "";
		if (selectAllByTestNo.getTestDate() != null) {
			Date fomatLocalDateTimeToDate4 = selectAllByTestNo.getTestDate();
			testDateString = TimeUtil.fomatDate(fomatLocalDateTimeToDate4);
		}
		pclrTestInvoicePrint.setTestDate(testDateString);
		pclrTestInvoicePrint.setTestFile(selectAllByTestNo.getTestFile());
		// 暂时不确定是否存值
		pclrTestInvoicePrint.setTestResult("");
//		try {
////			Map<String, Object> beanToMap = BeanManager.beanToMap(pclrTestInvoicePrint);
////			Connection connection = baseConnectionData.getConnection();
////			// 生成PDF
////			JasperRunManager.runReportToPdfFile("src/main/resources/config/pclrTestInvoice.jasper",
////					"F:\\Download\\pclrTestInvoice.pdf", beanToMap, connection);
////			FileInputStream fileInputStream = new FileInputStream(
////					new File("src/main/resources/config/pclrTestInvoice.jasper"));
////			runReportToPdf = JasperRunManager.runReportToPdf(fileInputStream, beanToMap, connection);
//			
//		} catch (JRException e) {
//			e.printStackTrace();
//		}
		return pclrTestInvoicePrint;
	}

	// 打印NDT试验单
	public PclrTestInvoicePrint printNdtTestInvoice(String testNo) {
	
		if (StringUtils.isEmpty(testNo)) {
			throw new RuntimeException("无法获取NDT表单编号！");
		}
		QualityPclrTestInvoiceEntity selectAllByTestNo = this.selectAllByTestNo(testNo);
		if (selectAllByTestNo == null) {
			throw new RuntimeException("无法获取检验单数据！");
		}
		PclrTestInvoicePrint pclrTestInvoicePrint = new PclrTestInvoicePrint();
		String auditDateString = "";
		if (selectAllByTestNo.getAuditDate() != null) {
			Date fomatLocalDateTimeToDate = selectAllByTestNo.getAuditDate();
			auditDateString = TimeUtil.fomatDate(fomatLocalDateTimeToDate);
		}
		pclrTestInvoicePrint.setApproved(selectAllByTestNo.getApproved());
		pclrTestInvoicePrint.setDepartment(selectAllByTestNo.getDepartment());
		pclrTestInvoicePrint.setIsConform(selectAllByTestNo.getIsConform());
		pclrTestInvoicePrint.setIsExtraUrgent(selectAllByTestNo.getIsExtraUrgent() == 1 ? "是" : "否");
		pclrTestInvoicePrint.setIsUrgent(selectAllByTestNo.getIsUrgent() == 1 ? "是" : "否");
		pclrTestInvoicePrint
				.setLotSize(selectAllByTestNo.getLotSize() == null ? "" : selectAllByTestNo.getLotSize().toString());
		pclrTestInvoicePrint.setMaterialDetail(selectAllByTestNo.getMaterialDetail());

		String needCompleteDateString = selectAllByTestNo.getNeedCompleteDate() == null ? ""
				: TimeUtil.fomatDate(selectAllByTestNo.getNeedCompleteDate());
		pclrTestInvoicePrint.setNeedCompleteDate(needCompleteDateString);

		pclrTestInvoicePrint.setOrderNo(selectAllByTestNo.getOrderNo());
		pclrTestInvoicePrint.setPclrNo(selectAllByTestNo.getTestNo());
		pclrTestInvoicePrint.setPhone(selectAllByTestNo.getPhone());
		pclrTestInvoicePrint.setPrepareUser(selectAllByTestNo.getPrepareUser());
		pclrTestInvoicePrint.setQarNo(selectAllByTestNo.getQarNo());
		pclrTestInvoicePrint.setSeller(selectAllByTestNo.getSeller());
		pclrTestInvoicePrint.setStatus(selectAllByTestNo.getStatus());
		String testDateString = "";
		if (selectAllByTestNo.getTestDate() != null) {
			Date fomatLocalDateTimeToDate4 = selectAllByTestNo.getTestDate();
			testDateString = TimeUtil.fomatDate(fomatLocalDateTimeToDate4);
		}
		pclrTestInvoicePrint.setTestDate(testDateString);
		pclrTestInvoicePrint.setTestFile(selectAllByTestNo.getTestFile());
		pclrTestInvoicePrint.setTestResult(selectAllByTestNo.getTestResult());
		return pclrTestInvoicePrint;
	}

	// 通过实验数据管理系统的参数更新数据
	public ResultBean updateByTestDateManage(String testNo, String testResult, String approved,
			List<QualityTestContentEntity> TestContentEntityList) {
		ResultBean resultBean = new ResultBean(true);
		// 查询pclr表单数据
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("test_no", testNo);
		QualityPclrTestInvoiceEntity selectOne = qualityPclrTestInvoiceDao.selectOne(queryWrapper);
		selectOne.setApproved(approved);
		selectOne.setTestResult(testResult);
		baseEntityDataUtil.updateBaseEntityData(selectOne);
		// 更新pclr数据
		qualityPclrTestInvoiceDao.updateById(selectOne);

		// 新增实验内容数据
		TestContentEntityList.forEach(item -> {
			baseEntityDataUtil.setBaseEntityData(item);
		});
		qualityTestContentDataHandler.saveBatch(TestContentEntityList, TestContentEntityList.size());
		return resultBean;
	}

	// 通过实验数据管理系统的参数更新数据
	public ResultBean updateByTestPclrDateManage(QualityTestContentEntity bean) {
		ResultBean resultBean = new ResultBean(true);
		// 查询pclr表单数据
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("test_no", bean.getTestNo());
		QualityPclrTestInvoiceEntity selectOne = qualityPclrTestInvoiceDao.selectOne(queryWrapper);
		if(selectOne==null){
			resultBean.setSuccess(false);
			resultBean.setErrorMessge("实验单号不存在");
			return resultBean;
		}
		//设置实验完成
		selectOne.setStatus(MomConstants.QUALITY_PCLR_STATUS_COMPLETE);
		baseEntityDataUtil.updateBaseEntityData(selectOne);
		// 更新pclr数据
		qualityPclrTestInvoiceDao.updateById(selectOne);
		// 新增实验内容数据
		baseEntityDataUtil.setBaseEntityData(bean);
		qualityTestContentDataHandler.save(bean);
		return resultBean;
	}

	// 通过实验管理系统传输的收料人时间更新试验单据
	public ResultBean updateReceiveDataByTestNo(String testNo, String receiveUser, String receiveDate,
			String deliverUser, String deliverDate) {
		ResultBean resultBean = new ResultBean(true);
		// 查询pclr表单数据
		QueryWrapper<QualityPclrTestInvoiceEntity> queryWrapper = new QueryWrapper<QualityPclrTestInvoiceEntity>();
		queryWrapper.eq("test_no", testNo);
		QualityPclrTestInvoiceEntity selectOne = qualityPclrTestInvoiceDao.selectOne(queryWrapper);
		selectOne.setReceiveUser(receiveUser);
		Date formatStringToDate = TimeUtil.formatStringToDate(receiveDate);
		selectOne.setReceiveDate(formatStringToDate);
		selectOne.setDeliverUser(deliverUser);
		Date formatStringToLocalDateTime = TimeUtil.formatStringToDate(deliverDate);
		selectOne.setDeliverDate(formatStringToLocalDateTime);
		baseEntityDataUtil.updateBaseEntityData(selectOne);
		qualityPclrTestInvoiceDao.updateById(selectOne);
		return resultBean;
	}

	public void initPclrTestInvoice(List<QualityQarBasicStandardRequirement> requirementList,QualityQarBasicStandardEntity innerData){
		{
			//查询需要补充的数据
			List<QualityPclrTestInvoiceEntityBean> pclrList=
					qualityPclrTestInvoiceDao.selectPclrTestInvoiceInfo(innerData.getId());
			Map pclrMap=new HashMap<String,QualityPclrTestInvoiceEntityBean>();
			Map<String,QualityPclrTestInvoice> tempMap=new HashMap<String,QualityPclrTestInvoice>();
				// 验证实验要求
			if (pclrList != null && pclrList.size() > 0&&requirementList != null && requirementList.size() > 0) {
				// PCLR检验单、Ndt集合
				ArrayList<QualityPclrTestInvoice> listQualityPclrTestInvoice = new ArrayList<QualityPclrTestInvoice>();
				for(QualityPclrTestInvoiceEntityBean pclr:pclrList) {
					pclrMap.put(pclr.getQarNo()+"_"+pclr.getTestType(),pclr);
				}
                for (QualityQarBasicStandardRequirement requirement : requirementList) {
					if(MomConstants.QUALITY_QAR_REQUIREMENT_TYPE_TEST.equals(requirement.getInnerData().getRequirementType())) {
						for (QualityPclrTestInvoiceEntityBean pclr : pclrList) {
							String testName = requirement.getInnerData().getRequirementName();
							if (testName.equalsIgnoreCase("MAT") && !pclrMap.containsKey(pclr.getQarNo() + "_PCLR")) {
								// 说明要生成pclr实验单
								String pclrNo = pclr.getQarNo() + "-PCLR";
								// 预留生成PCLR检验单
								QualityPclrTestInvoice qualityPclrTestInvoice = new QualityPclrTestInvoice();
								QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = new QualityPclrTestInvoiceEntity();
								qualityPclrTestInvoiceEntity.setTestNo(pclrNo);
								qualityPclrTestInvoiceEntity.setQarNo(pclr.getQarNo());
								qualityPclrTestInvoiceEntity.setTestType("PCLR");
								qualityPclrTestInvoiceEntity.setMaterialType(pclr.getMaterialType());
								qualityPclrTestInvoiceEntity.setProductModel(innerData.getModel());
								qualityPclrTestInvoiceEntity.setFormType(pclr.getFormType());
								qualityPclrTestInvoiceEntity
										.setStatus(MomConstants.QUALITY_PCLR_STATUS_DISPATCH);// 调度中
								qualityPclrTestInvoiceEntity
										.setPrepareUser(innerData.getCreator());
								qualityPclrTestInvoiceEntity
										.setDepartment(innerData.getDepartment());
								qualityPclrTestInvoiceEntity.setTestFile(requirement.getInnerData().getRequirementName() + "-"
										+ requirement.getInnerData().getMatNumber() + "-" + requirement.getInnerData().getTestItemQuatity());
								qualityPclrTestInvoiceEntity.setTestDate(TimeUtil.getLocalDate());
								qualityPclrTestInvoiceEntity.setMaterialDetail("零件名称:"
										+ innerData.getPartName() + "; 零件号:"
										+ innerData.getPartNumber() + "; 更改字母 :"
										+ innerData.getChangedLetters() + "; 状态:"
										+ innerData.getMaterialStatus() + "; 规范/标准:"
										+ innerData.getStandard() + "; 规范/标准版次:"
										+ innerData.getStandardVersion() + "; 工艺材料编号:"
										+ innerData.getRouteMaterialNumber() + "; 型别:"
										+ innerData.getType() + "; 类别/组合物别:级别:"
										+ innerData.getCategory() + "; 形式/形态:"
										+ innerData.getShape() + "; 规格:"
										+ innerData.getNorms() + "; 色号:"
										+ innerData.getColorCode() + "; 性能要求:"
										+ innerData.getPerformanceRequirement()
										+ "; 热处理/炉/批号:" + pclr.getBatchNumber()// 入库单来
										+ "; 试样数量 :" + innerData.getSamplesQuantity()
										+ "; 试样规格:" + innerData.getSamplesSize());
								qualityPclrTestInvoice.setInnerData(qualityPclrTestInvoiceEntity);
								listQualityPclrTestInvoice.add(qualityPclrTestInvoice);
							}
							if (!testName.equalsIgnoreCase("MAT") && !pclrMap.containsKey(pclr.getQarNo() + "_NDT")) {
								// 说明要生成NDT实验单
								QualityPclrTestInvoice qualityPclrTestInvoice = null;
								//先判断本次有没有生成NDT单子生成了 获取 然后添加内部的requirement
								if (tempMap.containsKey(pclr.getQarNo() + "_NDT")) {
									qualityPclrTestInvoice = tempMap.get(pclr.getQarNo() + "_NDT");
								} else {
									qualityPclrTestInvoice = new QualityPclrTestInvoice();
									String ndtNo = pclr.getQarNo() + "-NDT";
									QualityPclrTestInvoiceEntity qualityPclrTestInvoiceEntity = new QualityPclrTestInvoiceEntity();
									qualityPclrTestInvoiceEntity.setTestNo(ndtNo);
									qualityPclrTestInvoiceEntity.setQarNo(pclr.getQarNo());
									qualityPclrTestInvoiceEntity.setTestType("NDT");
									qualityPclrTestInvoiceEntity.setMaterialType(pclr.getMaterialType());
									qualityPclrTestInvoiceEntity.setProductModel(innerData.getModel());
									qualityPclrTestInvoiceEntity.setFormType(pclr.getFormType());
									qualityPclrTestInvoiceEntity.setStatus(MomConstants.QUALITY_PCLR_STATUS_DISPATCH);// 调度中
									qualityPclrTestInvoiceEntity
											.setPrepareUser(innerData.getCreator());
									qualityPclrTestInvoiceEntity
											.setDepartment(innerData.getDepartment());
									qualityPclrTestInvoiceEntity.setTestDate(TimeUtil.getLocalDate());
									qualityPclrTestInvoiceEntity.setMaterialDetail(
											"零件名称:" + innerData.getPartName() + "; 零件号:"
													+ innerData.getPartNumber() + "; 更改字母 :"
													+ innerData.getChangedLetters() + "; 状态:"
													+ innerData.getMaterialStatus() + "; 规范/标准:"
													+ innerData.getStandard() + "; 规范/标准版次:"
													+ innerData.getStandardVersion() + "; 工艺材料编号:"
													+ innerData.getRouteMaterialNumber() + "; 型别:"
													+ innerData.getType() + "; 类别/组合物别:级别:"
													+ innerData.getCategory() + "; 形式/形态:"
													+ innerData.getShape() + "; 规格:"
													+ innerData.getNorms() + "; 色号:"
													+ innerData.getColorCode() + "; 性能要求:"
													+ innerData.getPerformanceRequirement()
													+ "; 热处理/炉/批号:" + pclr.getBatchNumber()// 入库单来
									);
									qualityPclrTestInvoice.getInnerData().setTestFile("");
									qualityPclrTestInvoice.setInnerData(qualityPclrTestInvoiceEntity);
								}
								QualityPclrTestInvoiceDetailEntity detailEntity = new QualityPclrTestInvoiceDetailEntity();
								detailEntity.setRequirementType(requirement.getInnerData().getRequirementType());
								detailEntity.setQarBasicStandardId(requirement.getInnerData().getQarBasicStandardId());
								detailEntity.setRequirementName(requirement.getInnerData().getRequirementName());
								detailEntity.setMatNumber(requirement.getInnerData().getMatNumber());
								detailEntity.setTestItemQuatity(requirement.getInnerData().getTestItemQuatity());
								detailEntity.setRemark(requirement.getInnerData().getRemark());
								detailEntity.setAircraftLimit(requirement.getInnerData().getAircraftLimit());
								detailEntity.setOther(requirement.getInnerData().getOther());
								qualityPclrTestInvoice.getDetailEntityList().add(detailEntity);
								listQualityPclrTestInvoice.add(qualityPclrTestInvoice);
								qualityPclrTestInvoice.getInnerData().setTestFile(qualityPclrTestInvoice.getInnerData().getTestFile() + ";");
								tempMap.put(pclr.getQarNo() + "_NDT", qualityPclrTestInvoice);
							}
						}
					}
					}
				// 保存PCLR和NDT检验单
				if (listQualityPclrTestInvoice != null && listQualityPclrTestInvoice.size() > 0) {
					this.insertBatch(listQualityPclrTestInvoice);
				}
			}
		}
	}
}
